<?php

/**
 * Description of SoBizArticle
 *
 * @author Alen
 * @since 1.0.0 2013-12-14 sutroon
 */
class SoBizArticle extends SoBizBase
{

    //put your code here
    public function __construct()
    {
        parent::__construct();
        $this->dm->tablename = 'tgeneralarticle';
    }

    /**
     * 读取一条记录
     * @param type $id
     * @param type $search
     * @since 1.0.0 2013-12-14
     */
    public function read_one($id, $search = 'ID=%d')
    {
        // 读取数据
        if ($id)
        {
            return $this->dm->select_one_row('', sprintf($search, $id));
        }
        return null;
    }

    /**
     * 
     * @return type
     * @since 1.0.0 2013-12-14
     */
    public function save()
    {
        $this->dm->add_param('Title', request_string('title'), '标题不能为空');
        $this->dm->add_param('Content', request_string('content'), '内容不能为空');
        if ($this->dm->has_error())
            return $this->dm->get_error();

        $id = request_numeric('id');
        if ($id > 0)
        {
            $this->dm->update('id=' + $id);
        } else
        {
            $this->dm->insert();
        }
        return $this->dm->has_error() ? $this->dm->get_error() : 'SUCCESS';
    }

    /**
     * 删除一条记录
     * @return type
     * @since 1.0.0 2014-3-22 sutroon
     */
    public function remove()
    {
        $id = request_numeric('id');
        return $this->delete_one($id);
    }

    /**
     * 获取分页数据
     * @param type $pager_text
     * @param type $dataset
     * @param type $pageid
     * @param type $keyword
     * @since 1.0.0 2012-12-14 sutroon
     * @example 
     *  $pager_text='';
      $dataset=null;
      $biz->get_paging_data($pager_text, $dataset, request_numeric('page'), request_string('keyword'));
     */
    public function get_paging_data(&$pager_text, &$dataset, $pageid, $keyword)
    {
        // 获取分页数据
        $pager = new SoDataPager($pageid);
        $sqlc = $keyword ? ' where Title like \'%' . $keyword . '%\' ' : '';
        $dataset = $this->dm->select_pagingset($pager->pageid, $pager->pagesize, $pager->recordcount, 'tgeneralarticle ' . $sqlc, 'CreatedTime desc', 'ID, Title, VisitCount, State, CreatedTime');
        $pager->set_dataset($dataset, $pager->recordcount);
        $pager_text = $pager->get_pager_text(set_url_param(array('page' => '.pageid.')));
    }

}
